package com.zcf.mappers;

import com.fasterxml.jackson.databind.JsonNode;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.ListenableFuture;
import com.ning.http.client.Response;
//import com.sun.deploy.net.HttpUtils;

import java.util.concurrent.TimeUnit;

//import org.mortbay.util.UrlEncoded;

/**
 * 通过java api从高德地图获取经纬度 address 位置 output 返回结果格式 key 高德key值，需申请
 */
public class Demo {
//	public static void main(String[] args) {
//		String address = "上海市浦东新区东方明珠";
//		// 1、通过java api从高德地图获取经纬度
//		String url = "http://restapi.amap.com/v3/geocode/geo?address=" + address
//				+ "&output=JSON&key=04fae9b07d8abd27177207d1abc8bca6";
//
//		AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
////	        builder.setCompressionEnabled(true).setAllowPoolingConnection(true);
////	        builder.setRequestTimeoutInMs((int) TimeUnit.MINUTES.toMillis(1));
////	        builder.setIdleConnectionTimeoutInMs((int) TimeUnit.MINUTES.toMillis(1));
//
//		AsyncHttpClient client = new AsyncHttpClient(builder.build());
//		try {
//			ListenableFuture<Response> future = client.prepareGet(url).execute();
//			String result = future.get().getResponseBody();
//			System.out.println(result);
//			JsonNode jsonNode = new com.fasterxml.jackson.databind.ObjectMapper()
//					.readTree(future.get().getResponseBody());
//			if (jsonNode.findValue("status").textValue().equals("1")) {
//				JsonNode listSource = jsonNode.findValue("location");
//				System.out.println(listSource);
//				String appid = listSource.toString();
//				appid = appid.substring(0, appid.length() - 1);
//				appid = appid.substring(1, appid.length());
//				System.err.println(appid);
//				String id[] = appid.split(",");
//				System.err.println(id.length);
//				for (String string : id) {
//					System.err.println(string);
//				}
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			if (client != null) {
//				client.close();
//			}
//		}
//	}
	public static void main(String[] args) {
		String filename = "江苏省徐州市邳州市运河镇";
		String levelCode = filename.substring(filename.indexOf("省") + 1).trim();// 逗号后面
		String levelCodes = levelCode.substring(0, levelCode.indexOf("市")+1).trim();// 逗号前面
		levelCodes = levelCodes.substring(0,levelCodes.length() - 1);
		System.err.println(levelCodes);
	}

	public Object gaodeditu(String address) {
		// 1、通过java api从高德地图获取经纬度
		String url = "http://restapi.amap.com/v3/geocode/geo?address=" + address
				+ "&output=JSON&key=04fae9b07d8abd27177207d1abc8bca6";
		AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
//	        builder.setCompressionEnabled(true).setAllowPoolingConnection(true);
//	        builder.setRequestTimeoutInMs((int) TimeUnit.MINUTES.toMillis(1));
//	        builder.setIdleConnectionTimeoutInMs((int) TimeUnit.MINUTES.toMillis(1));
		AsyncHttpClient client = new AsyncHttpClient(builder.build());
		try {
			ListenableFuture<Response> future = client.prepareGet(url).execute();
			String result = future.get().getResponseBody();
			System.out.println(result);
			JsonNode jsonNode = new com.fasterxml.jackson.databind.ObjectMapper()
					.readTree(future.get().getResponseBody());
			if (jsonNode.findValue("status").textValue().equals("1")) {
				JsonNode listSource = jsonNode.findValue("location");
				System.out.println(listSource);
				String appid = listSource.toString();
				appid = appid.substring(0, appid.length() - 1);
				appid = appid.substring(1, appid.length());
				return appid;
//				for (String location : listSource.textValue().split(",")) {
//					// 得到这个位置的经纬度
//					System.out.println(location);
//					
//					// System.out.println(Double.valueOf(location));
//				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (client != null) {
				client.close();
			}
		}
		return null;
	}
}
